import db from "@/db/index";
const problemData = db.problem;
import { wxShare } from "@/utils";
export default {
    data() {
        return {
            pageList: [],
            mySwiper: {},
            currentIndex: 0,
            allRightNum: 0,
            setTimeLock: false,
            req: {},
            questionReq: {
                ok_num_flag: 0,
                no_num_flag: 0,
                null_num_flag: 0,
                content: ''
            }
        };
    },
    methods: {
        // 进入下一题
        nextQuestion() {
            try {
                let pageData = this.pageList[this.currentIndex]
                this.currentIndex++
                if (this.currentIndex == 10) {
                    this.$router.push({
                        path: '/buildImg',
                        query: { allRightNum: this.questionReq.ok_num_flag }
                    })
                }
                this.questionReq.content = this.questionReq.content + `{${this.currentIndex}|${pageData.result}|${pageData.curSelect ? pageData.curSelect : 'null'}},`
                if (pageData.isSelected) {
                    this.mySwiper.slideNext()
                } else {
                    pageData.isSelected = true
                    if (this.setTimeLock) return
                    this.questionReq.null_num_flag++
                    this.setTimeLock = true
                    window.setTimeout(_ => {
                        this.setTimeLock = false
                        this.mySwiper.slideNext()
                    }, 1000)
                }
                this.nextReqData()
            } catch (error) {
                alert(JSON.stringify(error))
            }
        },
        // 选择答案
        selectHandler(it, idx) {
            let pageData = this.pageList[this.currentIndex]

            if (this.setTimeLock) return
            if (pageData.isSelected) return
            pageData.curIdx = idx
            // 判断是否答对
            if (it.value == pageData.result) {
                pageData.isRight = true
                this.questionReq.ok_num_flag++
                console.log(this.questionReq.ok_num_flag);

            } else {
                pageData.isRight = false
                this.questionReq.no_num_flag++
            }
            pageData.curSelect = it.value
            pageData.isSelected = true

        },
        share() {
            wxShare({
                title: "这些常用药99%的人都用错了，测一测你都用对了么？",
                text: "", //`10道常用药安全测试题，99%的人都答错，无知很可怕，测一测你的用药安全意识`,
                href: 'http://app.allclinic.cn/h5/wx/startActivity/#/?source_type=3',
                imgUrl:
                    "http://app.allclinic.cn/h5/wx/startActivity/static/img/wechat_1.jpg",
                appMessageSuccFn: () => {
                    this.shareHandle()
                },
                timeLineSuccFn: () => {
                    this.shareHandle()
                }
            });
        },
        nextReqData() {
            this.$$api_global_question({
                data: {
                    ...this.req,
                    ...this.questionReq
                },
                noLoading: true
            })

        },
        shareHandle(){
            this.$$api_global_question({
                data: {
                    ...this.req,
                    transmit_flag: 1
                },
                noLoading: true
            })
          }
    },
    created() {

    },
    mounted() {
        function randomsort(a, b) {
            return Math.random() > .5 ? -1 : 1;
        }

        problemData.sort(randomsort)
        problemData.length = 10
        this.pageList = problemData.map(item => {
            item.isSelected = false
            item.curIdx = null
            item.isRight = false
            return item
        })
        this.$nextTick(function () {
            this.mySwiper = new Swiper(".swiper-container", {
                direction: "vertical",
                height: window.innerHeight,
                allowTouchMove: false,
            });
        })


        this.req = JSON.parse(window.localStorage.getItem('reqData'))
        this.share()
    }
};
